#include "head.h"

char* file_type(mode_t md)
{
	if(S_ISREG(md))
	{
		return "-";	
	}else if(S_ISDIR(md))
	{
		return "d";
	}else if(S_ISFIFO(md))
	{
		return "p";
	}else 
	{
		return "o" ;
	}
}

void do_cd(ptrain_t pt) 
{
	char dir[128]= "";
	sscanf(pt->buf +3, "%s", dir);//将ps ->sess_buf +3位置的数据读入dir
	chdir(dir);
	getcwd(dir, 128);
	pt->data_len = strlen(dir);
	send(pt->fd, &pt->data_len, sizeof(int), 0);
	sendn(pt->fd, dir, pt->data_len);
}

void do_ls(ptrain_t pt) 
{
	DIR* pdir = opendir("./");
    if(!pdir)
	{
		perror("opendir");
		return ;
	}
	struct dirent* mydir ;
    while( (mydir = readdir(pdir)) != NULL)//读取目录
    {
        if(strncmp(mydir->d_name, ".", 1) == 0 || strncmp(mydir->d_name,"..", 2) == 0)
        {
            continue ;
        }
        struct stat mystat;
        bzero(&mystat, sizeof(stat));
        stat(mydir->d_name, &mystat);//获取某个文件的信息
        bzero(pt->buf,sizeof(pt->buf));
        sprintf(pt->buf, "%-2s%-20s %10ldB",file_type(mystat.st_mode),mydir->d_name, mystat.st_size);
        pt->data_len =  strlen(pt->buf);
        send(pt->fd, &pt->data_len, sizeof(int), 0);//发送数据给客户端
        sendn(pt->fd, pt->buf, pt->data_len);
    }
    pt->data_len = 0 ;
    send(pt->fd, &pt->data_len, sizeof(int), 0);
}

void do_pwd(ptrain_t pt) 
{
    bzero(pt->buf, sizeof(pt->buf));
	getcwd(pt->buf, sizeof(pt->buf));
	pt->data_len = strlen(pt->buf);
	send(pt->fd, &pt->data_len, sizeof(int), 0);
	sendn(pt->fd, pt->buf, pt->data_len);
}

void do_rm(ptrain_t pt) 
{
	char cmd[128]={0};
	bzero(cmd,sizeof(cmd));
	sprintf(cmd,"rm -f %s",pt->buf+3);//一次删除一个
	system(cmd);//执行删除命令
	bzero(pt->buf,sizeof(pt->buf));
	sprintf(pt->buf, "%sremoved!",cmd+6);
	pt->buf[4]=' ';
	pt->data_len=strlen(pt->buf);
	send(pt->fd,&pt->data_len,sizeof(int),0);
	sendn(pt->fd,pt->buf,pt->data_len);
}

void do_puts(ptrain_t pt)
{
	char file_name[256];
	bzero(file_name, sizeof(file_name));
	strcpy(file_name,pt->buf+5);
	if(download(pt->fd, file_name) == 0)
	{
		printf("file -> %s download success \n", file_name);
	}else
	{

		printf("file -> %s download failed \n", file_name);
	}
}

void do_gets(ptrain_t pt) 
{
	char file_name[256];
	bzero(file_name, sizeof(file_name));
	sscanf(pt->buf+5 ,"%s", file_name);
	if(upload(pt->fd, file_name) == 0)
	{
		printf(" file-> %s upload success\n", file_name);
	}else 
	{
		printf(" file-> %s upload failed\n", file_name);
	}
}

int download(int fd, char*file_name)
{
	int fd_file = open(file_name, O_WRONLY|O_CREAT,0666 );
	if(fd_file == -1)
	{
		return -1 ;
	}
	char* write_buf = (char*)malloc(Num);
	bzero(write_buf, Num);
	int nwrite ;
	while(1)
	{
		recv(fd, &nwrite, sizeof(int), 0);
		if(nwrite == 0)
		{
			break ;
		}
		recvn(fd, write_buf, nwrite);
		writen(fd_file, write_buf, nwrite);
	}
	close(fd_file);
	return 0 ;
}

int upload(int fd,char* file_name)
{
	int fd_file = open(file_name, O_RDONLY);
	if(fd_file == -1)
	{
		return -1 ;
	}
	char *read_buf = (char*)malloc(Num);
	bzero(read_buf, Num);
	int nread ;
	while(1)
	{
		nread = readn(fd_file, read_buf, Num);
		if(nread < Num)
		{
			send(fd, &nread, sizeof(int), 0);
			sendn(fd, read_buf, nread);
			break ;
		}else
		{
			
			send(fd, &nread, sizeof(int), 0);
			sendn(fd, read_buf, nread);
		}
	}
	int flag = 0 ;
	send(fd, &flag, sizeof(flag), 0);
	close(fd_file);
	return 0 ;
}

int readn(int fd_read, char* read_buf, int len)
{
	int sum = 0 ;
	int nread ;
	while(sum < len)
	{
		nread = read(fd_read, &read_buf[sum], len - sum);
		if(nread == 0)
		{
			break ;
		}
		sum += nread ;
	}
	return sum ;		
}

int writen(int fd_write, char* write_buf, int len)
{
	int sum = 0 ;
	int nwrite ;
	while(sum < len)
	{
		nwrite = write(fd_write, write_buf + sum, len - sum);
		sum += nwrite ;
	}
	return sum ;
}